package com.atguigu.tingshu.order.strategy.impl;

import com.atguigu.tingshu.account.client.UserAccountFeignClient;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.order.mapper.OrderDerateMapper;
import com.atguigu.tingshu.order.mapper.OrderDetailMapper;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.order.strategy.SaveOrderStrategy;
import com.atguigu.tingshu.vo.account.AccountLockResultVo;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AccountSaveOrderStrategy implements SaveOrderStrategy {


    @Autowired
    private UserAccountFeignClient userAccountFeignClient;

    @Autowired
    private OrderInfoMapper orderInfoMapper ;

    @Autowired
    private OrderDetailMapper orderDetailMapper ;

    @Autowired
    private OrderDerateMapper orderDerateMapper ;

    @Autowired
    private OrderInfoService orderInfoService ;

    @Autowired
    private KafkaService kafkaService ;
    @Override
    public OrderInfoVo saveOrderInfo(OrderInfoVo orderInfoVo) {
        //1.0封装锁定的账户信息

        Long userId = AuthContextHolder.getUserId();
        AccountLockVo accountLockVo = new AccountLockVo();
        accountLockVo.setOrderNo(orderInfoVo.getTradeNo());
        accountLockVo.setUserId(userId);
        accountLockVo.setAmount(orderInfoVo.getOrderAmount());
        OrderDetailVo orderDetailVoList = orderInfoVo.getOrderDetailVoList().get(0);
        accountLockVo.setContent(orderDetailVoList.getItemName()); //Count 锁定内容在明细表中获取\

        //2.0 远程调用检查锁定和可用的余额
        Result<AccountLockResultVo> accountLockResultVoResult = userAccountFeignClient.checkAndLock(accountLockVo);
        Integer code = accountLockResultVoResult.getCode();
        if(!code.equals(200)){
            throw new GuiguException(ResultCodeEnum.ACCOUNT_LESS) ;
        }

        try{
            this.saveOrderInfoAndDetailAndDerateInfo(orderInfoVo , orderInfoMapper , orderDetailMapper , orderDerateMapper);
        }catch (Exception e){
            //发生异常 想kafka发送解锁锁定金额
            e.printStackTrace();
            kafkaService.sendMessage(KafkaConstant.QUEUE_ACCOUNT_UNLOCK , orderInfoVo.getTradeNo());        // 向kafka中发送解锁账户金额的消息
            throw new GuiguException(ResultCodeEnum.SERVICE_ERROR) ;
        }

        // 返回
        return orderInfoVo;
    }

    @Override
    public String getPayWay() {
        return SystemConstant.ORDER_PAY_ACCOUNT;
    }

    //在接口实现后续消息
    @Override
    public void saveOrderAfterProcess(OrderInfoVo orderInfoVo) {
        // 保存交易信息(user_paid_album、user_paid_track、user_vip_service) 定义一个后续操作的方法
        orderInfoService.orderPaySuccess(orderInfoVo.getTradeNo()) ;
        // 扣减账户的余额 发送到account的消息队列
        kafkaService.sendMessage(KafkaConstant.QUEUE_ACCOUNT_MINUS,orderInfoVo.getTradeNo());
    }
}
